﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using BSCMFF.Core.DataAccessLayer;

namespace BSCMFF.Web.Pages
{
    public partial class StrategicGoalList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            //if (e.Item is GridEditFormItem && e.Item.IsInEditMode)
            //{
            //    GridEditFormItem editedItem = (GridEditFormItem)e.Item;
            //    //DropDownList ddlp = (DropDownList)editedItem.FindControl("DDLPerspective");
            //    RadComboBox ddlp = (RadComboBox)editedItem.FindControl("DDLPerspective");

            //    RadComboBoxItem li = ddlp.Items[0];

            //    if (e.Item.OwnerTableView.IsItemInserted)
            //    {
            //        li.Enabled = false;
            //    }
            //    else
            //    {
            //        ddlp.Items.Remove(li);
            //    }               
            //}
            
        }

        protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
        {
            GridEditableItem newItem = e.Item as GridEditableItem;
            UserControl form = (UserControl)newItem.FindControl(GridEditFormItem.EditFormUserControlID);

            Strategic_Target st = new Strategic_Target();
            st.Name = (form.FindControl("TextBoxName") as RadTextBox).Text;
            st.Description = (form.FindControl("TextBoxDescription") as RadTextBox).Text;
            //st.Perspective_FK = Convert.ToInt32((form.FindControl("ComboBoxPerspective") as RadComboBox).SelectedValue);
            st.Start_Value = Convert.ToInt32((form.FindControl("NumericTextBoxStartValue") as RadNumericTextBox).Value);
            st.Start_Description = (form.FindControl("TextBoxStartDescription") as RadTextBox).Text;
            st.Target_Value = Convert.ToInt32((form.FindControl("NumericTextBoxTargetValue") as RadNumericTextBox).Value);
            st.Target_Description = (form.FindControl("TextBoxTargetDescription") as RadTextBox).Text;

            BSCEntities context = new BSCEntities();
            context.Strategic_Target.AddObject(st);
            context.SaveChanges();
        }

        protected void RadGrid1_DeleteCommand(object source, GridCommandEventArgs e)
        {
            GridEditableItem deleteItem = e.Item as GridEditableItem;
            int st_ID = Convert.ToInt32(deleteItem.OwnerTableView.DataKeyValues[deleteItem.ItemIndex]["Strategic_Target_ID"]);

            BSCEntities context = new BSCEntities();

            Strategic_Target st = (from strategic in context.Strategic_Target
                                   where strategic.Strategic_Target_ID == st_ID
                                   select strategic).Single();
            context.Strategic_Target.DeleteObject(st);
            context.SaveChanges();
        }

        protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
        {
            GridEditableItem editedItem = e.Item as GridEditableItem;
            UserControl form = (UserControl)editedItem.FindControl(GridEditFormItem.EditFormUserControlID);

            int st_ID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["Strategic_Target_ID"]);

            BSCEntities context = new BSCEntities();

            Strategic_Target st = (from strategic in context.Strategic_Target
                                   where strategic.Strategic_Target_ID == st_ID
                                   select strategic).Single();

            st.Name = (form.FindControl("TextBoxName") as RadTextBox).Text;
            st.Description = (form.FindControl("TextBoxDescription") as RadTextBox).Text;
            //st.Perspective_FK = Convert.ToInt32((form.FindControl("ComboBoxPerspective") as RadComboBox).SelectedValue);
            st.Start_Value = Convert.ToInt32((form.FindControl("NumericTextBoxStartValue") as RadNumericTextBox).Value);
            st.Start_Description = (form.FindControl("TextBoxStartDescription") as RadTextBox).Text;
            st.Target_Value = Convert.ToInt32((form.FindControl("NumericTextBoxTargetValue") as RadNumericTextBox).Value);
            st.Target_Description = (form.FindControl("TextBoxTargetDescription") as RadTextBox).Text;
            context.SaveChanges();
        } 
    }
}